from django.db import models

# Create your models here.
# 资源表
class ResourceModel(models.Model):
    name = models.CharField(max_length=10, unique=True, default="默认模块")
    pid = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='son')
    url = models.CharField(max_length=20, unique=True)
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = "资源表"
        verbose_name_plural = "资源表"
        db_table = 'resource'

# 角色表
class RoleModel(models.Model):
    name = models.CharField(max_length=10,unique=True ,default="角色")
    resource = models.ManyToManyField(ResourceModel)
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = "角色表"
        verbose_name_plural = "角色表"
        db_table = 'role'

# 用户表
class UserModel(models.Model):
    username = models.CharField(max_length=10, default='默认用户名')
    phone = models.CharField(max_length=11, unique=True)
    password = models.CharField(max_length=10, default='123')
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE,default=2)
    def __str__(self):
        return self.username
    class Meta:
        verbose_name = "用户表"
        verbose_name_plural = "用户表"
        db_table = 'user'

# 接口权限表：
class InterfacePerModel(models.Model):
    resource = models.ManyToManyField(ResourceModel,related_name="interfaces")
    url = models.CharField(max_length=20, unique=True)
    def __str__(self):
        return self.url
    class Meta:
        verbose_name = '接口权限表'
        verbose_name_plural = '接口权限表'
        db_table = 'interface_per'